# -*-ecoding:utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
import json
import logging
from . import get_DNS_ip_msg
from datetime import timedelta
from django.core.signing import TimestampSigner
from django.core import signing

# Create your views here.
log = logging.getLogger('django')


def add_DNS_IP(request):
    if request.method == "POST":
        msg = json.loads(request.body)
        ip = msg['ip']
        status = get_DNS_ip_msg.get_msg(ip)
        rjs = {}
        print(status)
        if status == 0:
            rjs['success'] = 0
            rjs['message'] = "录入失败，地址不符合要求或已经录入"
            rjs['data'] = ""
        else:
            rjs['success'] = 1
            rjs['message'] = "DNS地址录入成功"
            rjs['data'] = ""
    return HttpResponse(json.dumps(rjs), content_type="application/json")


def get_dns_addr(request):
    rjs = {}
    if request.method == "GET":
        msg = get_DNS_ip_msg.get_addr()
        rjs['success'] = 1
        rjs['message'] = ""
        rjs['data'] = msg
    return HttpResponse(json.dumps(rjs), content_type="application/json")


def select_DNS(request):
    rjs = {}
    if request.method == "POST":
        msg = json.loads(request.body)
        domain_name = msg['domain_name']
        dns_type = msg['dns_type']
        dns_id = msg['dns_id']
        # print(domain_name)
        # print(dns_type)
        # print(dns_id)
        IP_msg = get_DNS_ip_msg.get_dns_msg(int(dns_id), domain_name, dns_type)
        rjs['success'] = 1
        rjs['message'] = ""
        rjs['data'] = IP_msg
    return HttpResponse(json.dumps(rjs), content_type="application/json")


def Token(request):
    # signer = TimestampSigner()
    # value = signer.sign('hello')
    value = signing.dumps({"user": "user1"})
    print(value)
    print(signing.loads(value))
    return JsonResponse({'token': value})


def SingleElection(request):
    if request.method == "GET":
        data = {
            "SingleElection": {
                "total": 2,
                "content": [
                    {
                        "num": 1,
                        "ask": '你喜欢喝什么？',
                        "answer": [
                            {"name": 'A', "value": '汽水'},
                            {"name": 'B', "value": '啤酒'},
                            {"name": 'C', "value": '咖啡'},
                            {"name": 'D', "value": '茶', "checked": 'true'},
                        ]
                    },
                    {
                        "num": 2,
                        "ask": '你喜欢吃什么？',
                        "answer": [
                            {"name": 'A', "value": '汉堡'},
                            {"name": 'B', "value": '炸鸡', "checked": 'true'},
                            {"name": 'C', "value": '派'},
                            {"name": 'D', "value": '烤牛肉'},
                        ]
                    }
                ]
            },
            "checked": 'toCheck',
            "hiddenStatus": 'true',
            "askNum": 0

        }
        return JsonResponse(data)
